//
//  MemoryGuard.m
//  etionMisc
//
//  Created by wjxfhxy on 14-4-24.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "MemoryGuard.h"

static CMemoryGuard *s_mg;

@implementation CMemoryGuard
{
    NSTimer *_timer;
}

-(void)dealloc
{
    [_timer invalidate];
    ReleaseObj(_timer);
    superdealloc_mm;
}

-(void)startMemoryGuard
{
    if(_timer==nil)
    {
        _timer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(checkMemory:) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
        [_timer fire];
    }
}

-(void)checkMemory:(NSTimer*)timer
{
    DispatchThread(^
    {
        NSUInteger m = [UIDevice currentAvailableMemory];
        if(m != NSNotFound && m<=200)
        {
            [self allocABigMemoryZone];
        }
    });
}

-(void)allocABigMemoryZone
{
    NSUInteger len = 1024*1024*500;
    char* c = malloc(len);
    for(NSUInteger i = 0 ; i<len; i++)
        c[i] = 'A';
    free(c);
}

+(void)startMemoryGuard
{
    if(s_mg==nil)
    {
        s_mg = [CMemoryGuard new];
        [s_mg startMemoryGuard];
    }
}

@end
